﻿using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ServiceControl.Helps
{
    public static class Extension
    {
        static Extension()
        {

        }
        public static T JsonFromString<T>(string str, List<Type> types = null, DefaultValueHandling defaultValueHandling = DefaultValueHandling.Ignore)
        where T : new()
        {
            T t;
            try
            {
                t = JsonConvert.DeserializeObject<T>(str, new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore,
                    DefaultValueHandling = defaultValueHandling,
                    TypeNameHandling = TypeNameHandling.Objects,
                    ObjectCreationHandling = ObjectCreationHandling.Replace
                });
            }
            catch (Exception exception)
            {
                return default(T);
            }
            return t;
        }

        //public static T JsonDeserialize<T>(string jsonString)
        //{
        //    T obj = Activator.CreateInstance<T>();
        //    try
        //    {
        //        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
        //        {
        //            DataContractJsonSerializer ser = new DataContractJsonSerializer(obj.GetType());//typeof(T)
        //            T jsonObject = (T)ser.ReadObject(ms);
        //            ms.Close();

        //            return jsonObject;
        //        }
        //    }
        //    catch
        //    {
        //        return default(T);
        //    }
        //}

        public static object JsonFromString(string str, Type target = null, List<Type> types = null, DefaultValueHandling defaultValueHandling = DefaultValueHandling.Ignore)
        {
            object obj;
            try
            {
                obj = JsonConvert.DeserializeObject(str, new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore,
                    DefaultValueHandling = defaultValueHandling,
                    TypeNameHandling = TypeNameHandling.Objects,
                    PreserveReferencesHandling = PreserveReferencesHandling.All,
                    ObjectCreationHandling = ObjectCreationHandling.Replace
                });
            }
            catch (Exception exception)
            {
                return null;
            }
            return obj;
        }



        public static string JsonToString(this object data, bool flag = true, bool QuoteName = true, Formatting format = 0, DefaultValueHandling defaultValueHandling = DefaultValueHandling.Ignore)
        {
            if (!QuoteName)
            {
                return data.Serialize(null, false, Formatting.None);
            }
            if (!flag)
            {
                return data.Serialize(null, true, Formatting.None);
            }
            string strJson = JsonConvert.SerializeObject(data, format, new JsonSerializerSettings()
            {
                NullValueHandling = NullValueHandling.Ignore,
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                DefaultValueHandling = defaultValueHandling,
                TypeNameHandling = TypeNameHandling.Objects,
                PreserveReferencesHandling = PreserveReferencesHandling.All
            });
            string testStr = strJson;
            return strJson;
        }


        public static string Serialize(this object o, IContractResolver resolver = null, bool quoteName = false, Formatting format = Formatting.None)
        {
            StringWriter stringWriter = new StringWriter();
            JsonTextWriter jsonWriter = new JsonTextWriter(stringWriter)
            {
                QuoteName = quoteName,
                Formatting = format,
                StringEscapeHandling = StringEscapeHandling.Default
            };
            (new JsonSerializer()
            {
                ContractResolver = resolver,
                NullValueHandling = NullValueHandling.Ignore,
                Formatting = format,
                TypeNameHandling = TypeNameHandling.None
            }).Serialize(jsonWriter, o);
            return stringWriter.ToString().Replace("---", "`").Replace("__", "/");
        }


        public static string JsonFormatOut(this string str)
        {
            JsonSerializer serializer = new JsonSerializer();
            object obj = serializer.Deserialize(new JsonTextReader(new StringReader(str)));
            if (obj == null)
            {
                return str;
            }
            StringWriter stringWriter = new StringWriter();
            serializer.Serialize(new JsonTextWriter(stringWriter)
            {
                Formatting = Formatting.Indented,
                Indentation = 4,
                IndentChar = ' '
            }, obj);
            return stringWriter.ToString();
        }




    }
}
